﻿@using LdCms.EF.DbModels
@using LdCms.Common.Utility
@using LdCms.Common.Extension
@model IEnumerable<Ld_Sys_Function>
@{
    ViewData["Title"] = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<article class="page-container">
    <form action="@Url.Action("Save")" method="post" class="form form-horizontal" id="form-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色编号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="fRoleId" name="fRoleId">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="fRoleName" name="fRoleName">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">备注：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="fRemark" name="fRemark">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">角色功能：</label>
            <div class="formControls col-xs-8 col-sm-9">
                @foreach (var m in Model)
                {
                    if (m.RankID == 1 && m.FunctionID != "000000")
                    {
                        <dl class="permission-list">
                            <dt>
                                <label><input type="checkbox" value="@m.FunctionID" name="fFuncId">@m.FunctionName</label>
                            </dt>
                            <dd>
                                @foreach (var m1 in Model.Where(w => w.ParentID == m.FunctionID))
                                {
                                    <dl class="cl permission-list2">
                                        <dt>
                                            <label class=""><input type="checkbox" value="@m1.FunctionID" name="fFuncId">@m1.FunctionName</label>
                                        </dt>
                                        <dd>
                                            @foreach (var m2 in Model.Where(w => w.ParentID == m1.FunctionID))
                                            {
                                                <label class=""><input type="checkbox" value="@m2.FunctionID" name="fFuncId">@m2.FunctionName</label>
                                            }
                                        </dd>
                                    </dl>
                               }
                            </dd>
                        </dl>
                    }
                 }
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">审核：</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <div class="check-box">
                    <input type="checkbox" id="fState" name="fState" value="1" checked="checked" />
                    <label for="checkbox-1">&nbsp;</label>
                </div>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button type="submit" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
            </div>
        </div>
    </form>
</article>

@*
    --------------
    页面底部JS脚本
    --------------
*@
@section scripts{
    <!--请在下方写此页面业务相关的脚本-->
    <script type="text/javascript" src="~/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="~/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
    <script type="text/javascript" src="~/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
    <script type="text/javascript">
        (function ($) {
            $.mainu = {
                init: function () {
                    $('.skin-minimal input').iCheck({
                        checkboxClass: 'icheckbox-blue',
                        radioClass: 'iradio-blue',
                        increaseArea: '20%'
                    });
                    $(".permission-list dt input:checkbox").click(function () {
                        $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
                    });
                    $(".permission-list2 dd input:checkbox").click(function () {
                        var l = $(this).parent().parent().find("input:checked").length;
                        var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
                        if ($(this).prop("checked")) {
                            $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                            $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
                        }
                        else {
                            if (l == 0) {
                                $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                            }
                            if (l2 == 0) {
                                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                            }
                        }
                    });
                },
                formSubmit: function () {
                    $("#form-add").validate({
                        rules: {
                            fRoleId: {
                                required: true,
                                minlength: 3,
                                maxlength: 4
                            },
                            fRoleName: {
                                required: true,
                                minlength: 2,
                                maxlength: 20
                            }
                        },
                        onkeyup: false,
                        focusCleanup: true,
                        success: "valid",
                        submitHandler: function (form) {
                            var fState = $("input[name='fState']").is(':checked');
                            $(form).ajaxSubmit({
                                type: "POST",
                                cache: false,
                                data: { fState: fState },
                                dataType: "json",
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    if (XMLHttpRequest.status != 200) {
                                        layer.alert("POST[FAIL]", { icon: 5 });
                                    }
                                },
                                success: function (result) {
                                    var state = result.state;          //错误代码
                                    var message = result.message;        //错误说明
                                    if (state == "success") {
                                        var index = parent.layer.getFrameIndex(window.name);
                                        parent.location.reload();
                                        parent.layer.close(index);
                                    } else {
                                        layer.alert(message, { icon: 5 });
                                    }
                                }
                            });
                        }
                    });
                }
            };
            $(function () {
                $.mainu.init();
                $.mainu.formSubmit();
            });

        })(jQuery);
    </script>
    <!--/请在上方写此页面业务相关的脚本-->


}

